//package com.bruce.practice.es.service;
//
//import org.apache.commons.collections4.MapUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.lucene.search.join.ScoreMode;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.unit.Fuzziness;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.MatchQueryBuilder;
//import org.elasticsearch.index.query.Operator;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.query.TermsQueryBuilder;
//import org.elasticsearch.join.query.HasChildQueryBuilder;
//import org.elasticsearch.join.query.JoinQueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.sort.FieldSortBuilder;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * @author luotuan
// * @Description
// * @create 2020-05-08 20:39
// **/
////@Service
//public class SearchService {
//    //@Autowired
//    private RestHighLevelClient restHighLevelClient;
//
//    public static final List<String> EXCLUDE_FIELD = new ArrayList<>();
//
//    static {
//        Collections.addAll(EXCLUDE_FIELD, "AREA_CODE","code","customer_order");
//    }
//
//    public void testSearch() {
//
//    }
//
//    public void searchByGird(int start, int limit) {
//
//    }
//
//    public void testInsert(){
//
//    }
//
//    /**
//     * 构建es 高级查询对象
//     *
//     * @param start
//     * @param limit
//     * @param paramMap
//     * @return
//     */
//    public void initSearchRequest(int start, int limit, Map<String, Object> paramMap) {
//        SearchRequest searchRequest = new SearchRequest("gy_oss_db*");
//        searchRequest.types("customer");
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        String custOrderCode = MapUtils.getString(paramMap, "custOrderCode");
//        String custOrderState = MapUtils.getString(paramMap, "custOrderState");
//        String startDate = MapUtils.getString(paramMap,"startDate");
//        String endDate = MapUtils.getString(paramMap,"endDate");
//        String accNbr = MapUtils.getString(paramMap,"accNbr");
//        String masterOrderState = MapUtils.getString(paramMap,"masterOrderState");
//
//        //创建复合查询对象(将查询条件进行and or 拼接)
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        QueryBuilder qb= JoinQueryBuilders.hasChildQuery("order",QueryBuilders.matchAllQuery(),ScoreMode.None);
//        boolQueryBuilder.must(qb);
//        if (StringUtils.isNotEmpty(custOrderCode)) {
//            boolQueryBuilder.must(QueryBuilders.wildcardQuery("custOrderCode", "*"+custOrderCode+"*"));
//        }
//        if(StringUtils.isNotEmpty(custOrderState)){
//            boolQueryBuilder.must(QueryBuilders.matchQuery("orderState",custOrderState));
//        }
//        if(StringUtils.isNotEmpty(startDate)){
//            boolQueryBuilder.must(QueryBuilders.rangeQuery("createDate").gte(startDate));
//        }
//        if(StringUtils.isNotEmpty(endDate)){
//            boolQueryBuilder.must(QueryBuilders.rangeQuery("createDate").lte(endDate));
//        }
//        if(StringUtils.isNotEmpty(accNbr)){
//            String childType = "order";
//            QueryBuilder matchQuery = QueryBuilders.matchQuery("accNbr", accNbr);
//            HasChildQueryBuilder childQueryBuilder = new HasChildQueryBuilder(childType,matchQuery, ScoreMode.Total);
//            boolQueryBuilder.must(childQueryBuilder);
//        }
//
//        if(StringUtils.isNotEmpty(masterOrderState)){
//            String childType = "order";
//            QueryBuilder matchQuery = QueryBuilders.matchQuery("serviceOrderState", masterOrderState);
//            HasChildQueryBuilder childQueryBuilder = new HasChildQueryBuilder(childType,matchQuery, ScoreMode.Total);
//            boolQueryBuilder.must(childQueryBuilder);
//        }
//        sourceBuilder.query(boolQueryBuilder);
//        sourceBuilder.from(start);
//        sourceBuilder.size(limit);
//        sourceBuilder.sort(new FieldSortBuilder("createDate").order(SortOrder.DESC));
//        System.out.println("查询的dsl："+sourceBuilder.toString());
//        searchRequest.source(sourceBuilder);
//        try {
//            SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
//            SearchHit[] searchHits = searchResponse.getHits().getHits();
//            for (SearchHit searchHit : searchHits) {
//                String json = searchHit.getSourceAsString();
//                Map<String,Object> result = searchHit.getSourceAsMap();
//                result.entrySet().removeIf(entry->("CODE".equals(entry.getKey())||"AREA_CODE".equals(entry.getKey()) || "customer_order".equals(entry.getKey())));
//                for(Map.Entry<String,Object> entry : result.entrySet()){
//                    String key = entry.getKey();
//                    if(key.contains("Date")){
//                        String value = (String) entry.getValue();
//                        if(StringUtils.isNotEmpty(value)){
//                            value = value.replace("T"," ").replace("+08:00","");
//                            result.put(key,value);
//                        }
//                    }
//
//                }
//                result.forEach((k,v)->{
//                    System.out.println("键："+k+"，值："+v);
//                });
//                System.out.println(searchHit.getSourceAsString());
//            }
//            //System.out.println();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    public static void main(String[] args) throws ParseException {
//        System.out.println(EXCLUDE_FIELD.size());
//    }
//}
